using System;
using System.Collections.Generic;
using System.Linq;
using ICSharpCode.TextEditor.Gui.CompletionWindow;

namespace ICSharpCode.TextEditor.Formatting.Intellisense
{
    public class JavascriptCompletionDataSupplier : ICompletionDataSupplier
    {
        private static object synicRoot = new object();

        private bool completionDataListInitialized = false;

        private ICompletionData[] DefaultCompletionDataList = null;

        private ICompletionData[] DocumentFunctionDataList = null;

        private ICompletionData[] JqueryFunctionDataList = null;

        private ICompletionData[] JqueryDocumentFunctionDataList = null;

        private ICompletionData[] JqueryDocFunctionDataList = null;

        private ICompletionData[] JsonFunctionDataList = null;

        private ICompletionData[] MathFunctionDataList = null;

        private ICompletionData[] consoleFunDataList = null;

        private string HandleKeyWord(string lineText, char charTyped)
        {
            string empty = string.Empty;
            List<char> list = new List<char>();
            char[] array = (lineText + charTyped).ToCharArray();
            int num = array.Length - 1;
            while (num >= 0 && array[num] != ' ' && array[num] != '(' && array[num] != '>')
            {
                list.Add(array[num]);
                num--;
            }
            list.Reverse();
            return string.Join("", list.ToArray()).Trim();
        }

        private void CompletionDataListInitialize()
        {
            DefaultCompletionDataList = new ICompletionData[19]
            {
                new FunctionCompletionData("alert", "", 0),
                new DefaultCompletionData("body", "", 0),
                new DefaultCompletionData("catch", "", 0),
                new DefaultCompletionData("console", "", 0),
                new DefaultCompletionData("window", "", 0),
                new DefaultCompletionData("document", "", 0),
                new DefaultCompletionData("false", "", 0),
                new DefaultCompletionData("function", "", 0),
                new DefaultCompletionData("head", "", 0),
                new DefaultCompletionData("JSON", "", 0),
                new DefaultCompletionData("require", "引入JS模块", 0),
                new DefaultCompletionData("return", "", 0),
                new DefaultCompletionData("style", "", 0),
                new DefaultCompletionData("success", "", 0),
                new DefaultCompletionData("title", "", 0),
                new DefaultCompletionData("true", "", 0),
                new DefaultCompletionData("try", "", 0),
                new FunctionCompletionData("parseInt", "", 0),
                new FunctionCompletionData("parseFloat", "", 0)
            };
            DocumentFunctionDataList = new ICompletionData[5]
            {
                new FunctionCompletionData("appendChild", "", 0),
                new FunctionCompletionData("getElementById", "", 0),
                new FunctionCompletionData("getElementsByName", "", 0),
                new FunctionCompletionData("getElementsByClassName", "", 0),
                new FunctionCompletionData("hasAttributes", "", 0)
            };
            JqueryFunctionDataList = new ICompletionData[5]
            {
                new JqueryFunctionCompletionData("ajax", "", 0),
                new JqueryFunctionCompletionData("each", "", 0),
                new JqueryFunctionCompletionData("get", "", 0),
                new JqueryFunctionCompletionData("getJSON", "", 0),
                new JqueryFunctionCompletionData("post", "", 0)
            };
            JqueryDocumentFunctionDataList = new ICompletionData[1]
            {
                new JqueryFunctionCompletionData("ready", "", 0)
            };
            JqueryDocFunctionDataList = new ICompletionData[21]
            {
                new StringCompletionData("addClass", "", 0),
                new StringCompletionData("append", "", 0),
                new StringCompletionData("appendTo", "", 0),
                new StringCompletionData("attr", "", 0),
                new StringCompletionData("css", "", 0),
                new StringCompletionData("click", "", 0),
                new StringCompletionData("empty", "", 0),
                new StringCompletionData("height", "", 0),
                new StringCompletionData("html", "", 0),
                new StringCompletionData("parent", "", 0),
                new StringCompletionData("remove", "", 0),
                new StringCompletionData("removeClass", "", 0),
                new StringCompletionData("resize", "", 0),
                new StringCompletionData("siblings", "", 0),
                new StringCompletionData("size", "", 0),
                new StringCompletionData("scrollTop", "", 0),
                new StringCompletionData("scrollLeft", "", 0),
                new StringCompletionData("text", "", 0),
                new StringCompletionData("toggleClass", "", 0),
                new StringCompletionData("val", "", 0),
                new StringCompletionData("width", "", 0)
            };
            JsonFunctionDataList = new ICompletionData[2]
            {
                new FunctionCompletionData("parse", "", 0),
                new FunctionCompletionData("stringify", "", 0)
            };
            MathFunctionDataList = new ICompletionData[10]
            {
                new StringCompletionData("cos", "", 0),
                new StringCompletionData("floor", "", 0),
                new StringCompletionData("pow", "", 0),
                new StringCompletionData("PI", "", 0),
                new StringCompletionData("random", "", 0),
                new StringCompletionData("sin", "", 0),
                new StringCompletionData("sqrt", "", 0),
                new StringCompletionData("log", "", 0),
                new StringCompletionData("min", "", 0),
                new StringCompletionData("max", "", 0)
            };
            consoleFunDataList = new ICompletionData[2]
            {
                new FunctionCompletionData("log", "", 0),
                new FunctionCompletionData("msg", "", 0)
            };
            completionDataListInitialized = true;
        }

        public ICompletionData[] GetCompletionData(string lastkeyword, char charTyped)
        {
            if (!completionDataListInitialized)
            {
                lock (synicRoot)
                {
                    if (!completionDataListInitialized)
                    {
                        CompletionDataListInitialize();
                    }
                }
            }
            if ((lastkeyword + charTyped).Trim().StartsWith("$(document)."))
            {
                IEnumerable<ICompletionData> enumerable = null;
                string keyword3 = (lastkeyword + charTyped).Trim();
                if (keyword3.StartsWith("$(document)."))
                {
                    keyword3 = keyword3.Substring(12);
                    enumerable = JqueryDocumentFunctionDataList.Where((ICompletionData w) => w.Text.StartsWith(keyword3, StringComparison.OrdinalIgnoreCase));
                }
                if (enumerable != null && enumerable.Count() > 0)
                {
                    return enumerable.ToArray();
                }
            }
            else if ((lastkeyword + charTyped).Trim().StartsWith("$(") && (lastkeyword + charTyped).Trim().IndexOf(").") > 1)
            {
                IEnumerable<ICompletionData> enumerable2 = null;
                string keyword2 = (lastkeyword + charTyped).Trim();
                string text = HandleKeyWord(lastkeyword, charTyped);
                if (text == "style.")
                {
                    return null;
                }
                keyword2 = keyword2.Substring(keyword2.LastIndexOf(".") + 1);
                enumerable2 = JqueryDocFunctionDataList.Where((ICompletionData w) => w.Text.StartsWith(keyword2, StringComparison.OrdinalIgnoreCase));
                if (enumerable2 != null && enumerable2.Count() > 0)
                {
                    return enumerable2.ToArray();
                }
            }
            lastkeyword = HandleKeyWord(lastkeyword, charTyped);
            if (!string.IsNullOrEmpty(lastkeyword))
            {
                IEnumerable<ICompletionData> enumerable3 = null;
                string keyword = lastkeyword;
                if (keyword.StartsWith("$."))
                {
                    keyword = keyword.Substring(2);
                    enumerable3 = JqueryFunctionDataList.Where((ICompletionData w) => w.Text.StartsWith(keyword, StringComparison.OrdinalIgnoreCase));
                }
                else if (keyword.StartsWith("Math."))
                {
                    keyword = keyword.Substring(5);
                    enumerable3 = MathFunctionDataList.Where((ICompletionData w) => w.Text.StartsWith(keyword, StringComparison.OrdinalIgnoreCase));
                }
                else if (keyword.StartsWith("document."))
                {
                    keyword = keyword.Substring(9);
                    enumerable3 = DocumentFunctionDataList.Where((ICompletionData w) => w.Text.StartsWith(keyword, StringComparison.OrdinalIgnoreCase));
                }
                else if (keyword.StartsWith("JSON."))
                {
                    keyword = keyword.Substring(5);
                    enumerable3 = JsonFunctionDataList.Where((ICompletionData w) => w.Text.StartsWith(keyword, StringComparison.OrdinalIgnoreCase));
                }
                else if (keyword.StartsWith("console."))
                {
                    keyword = keyword.Substring(8);
                    enumerable3 = consoleFunDataList.Where((ICompletionData w) => w.Text.StartsWith(keyword, StringComparison.OrdinalIgnoreCase));
                }
                else
                {
                    enumerable3 = DefaultCompletionDataList.Where((ICompletionData w) => w.Text.StartsWith(keyword, StringComparison.OrdinalIgnoreCase));
                }
                if (enumerable3 != null && enumerable3.Count() > 0)
                {
                    return enumerable3.ToArray();
                }
            }
            return null;
        }
    }
}
